package com.sun.tool.crypto;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * @Auther: Sunql
 * @Date: 2018/6/11 16:17
 * @Description:
 * BASE64Encoder 加密解密
 * c+也可以使用
 */
public class Base64 {


    public static String decodeKey(String code){

        String key;     //key必须16位
        switch (code) {
            case "A":
                key = "2018050620180506"; //A 加密/解密编码
                break;
            case "B":
                key = "2018050620180506"; //B 加密/解密编码
                break;
            case "C":
                key = "2018050620180506"; //C 加密/解密编码
                break;
            case "D":
                key = "2018050620180506"; //D 加密/解密编码
                break;
            default:
                key = "2018050620180506"; //默认解密编码
                break;
        }
        return key;
    }

    public static String aesEncrypt(String str, String key) throws Exception {
        if (str == null || key == null) return null;
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
        byte[] bytes = cipher.doFinal(str.getBytes("utf-8"));
        String aesStr = new BASE64Encoder().encode(bytes);
        aesStr =aesStr.replace('+', '-').replace('/', '_').replace("=", ".");
        return aesStr;
    }

    public static String aesDecrypt(String str, String key) throws Exception {
        if (str == null || key == null) return null;
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
        byte[] bytes = new BASE64Decoder().decodeBuffer(str);
        bytes = cipher.doFinal(bytes);
        return new String(bytes, "utf-8");
    }


    /**
     * 将需要加密的字符串加密
     * @author        Sunql
     * @time          2018/4/11 13:17
     * @param         cont 需要加密的内容
     * @param         code 秘钥
     * @return        解密后的内容
     * @mender
     * */
    public  static String  encryption(String cont,String code) throws Exception {
        cont = cont.replace('+', '-').replace('/', '_').replace("=", ".");
        cont = aesEncrypt(cont,decodeKey(code));
        return cont;
    }


    /**
     * 将加密内容 解密
     * @author        Sunql
     * @time          2018/4/11 13:17
     * @param         cont 需要解密的内容
     * @param         code 秘钥
     * @return        解密后的内容
     * @mender
     * */
    public static String decode(String cont,String code) throws Exception {
        cont = cont.replace('-', '+').replace('_', '/').replace(".", "=");
        cont = aesDecrypt(cont,decodeKey(code));
        return cont;
    }

    public static void main(String[] args) throws Exception {

        String ms8 = "小米明天要高考了";
        String ms9 = aesEncrypt(ms8, decodeKey("A"));
        System.out.println("ms9: + \n" + ms9);

        String ms11 = decode(ms9, "A");
        System.out.println("ms10: + \n" + ms11);

    }

}
